package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.web.controller.enums.ExamineFlagEnum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HealthRegister;
import com.ruoyi.system.service.IHealthRegisterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 健康登记Controller
 * 
 * @author Yayo
 * @date 2021-05-12
 */
@Controller
@RequestMapping("/system/health_register")
public class HealthRegisterController extends BaseController
{
    private String prefix = "system/health_register";

    @Autowired
    private IHealthRegisterService healthRegisterService;

    @RequiresPermissions("system:health_register:view")
    @GetMapping()
    public String health_register()
    {
        return prefix + "/health_register";
    }

    /**
     * 查询健康登记列表
     */
    @RequiresPermissions("system:health_register:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HealthRegister healthRegister)
    {
        startPage();
        SysUser sysUser = ShiroUtils.getSysUser();
        String power = "";
        healthRegister.getParams().put("flag", Sets.newHashSet());
        for(SysRole role : sysUser.getRoles()){
            if("normal_admin".equals(role.getRoleKey())){
                List<Integer> flag = Lists.newArrayList(ExamineFlagEnum.PRELIMINARY_REFUSE.getCode(),ExamineFlagEnum.SUBMIT.getCode(), ExamineFlagEnum.PRELIMINARY_EXAMINE.getCode(), ExamineFlagEnum.FINAL_EXAMINE.getCode());
                ((Set)healthRegister.getParams().get("flag")).addAll(flag);
                power += "little";
            }else if("senior_admin".equals(role.getRoleKey())){
                List<Integer> flag = Lists.newArrayList(ExamineFlagEnum.PRELIMINARY_EXAMINE.getCode(),ExamineFlagEnum.FINAL_REFUSE.getCode(), ExamineFlagEnum.FINAL_EXAMINE.getCode());
                ((Set)healthRegister.getParams().get("flag")).addAll(flag);
                power += "normal";
            }else if("admin".equals(role.getRoleKey())){
                //do nothing , select all
                ((Set)healthRegister.getParams().get("flag")).addAll(ExamineFlagEnum.getAllCode());
                power += "all";
            } else{
                ((Set)healthRegister.getParams().get("flag")).add(-1);
            }
        }
        List<HealthRegister> list = healthRegisterService.selectHealthRegisterList(healthRegister);

        for(HealthRegister hr : list){
            if(power.contains("little") && Objects.equals(ExamineFlagEnum.SUBMIT.getCode(), hr.getFlag())){
                hr.setCanExamine(true);
            }
            if(power.contains("normal") && Objects.equals(ExamineFlagEnum.PRELIMINARY_EXAMINE.getCode(), hr.getFlag())){
                hr.setCanExamine(true);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出健康登记列表
     */
    @RequiresPermissions("system:health_register:export")
    @Log(title = "健康登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HealthRegister healthRegister)
    {
        List<HealthRegister> list = healthRegisterService.selectHealthRegisterList(healthRegister);
        ExcelUtil<HealthRegister> util = new ExcelUtil<HealthRegister>(HealthRegister.class);
        return util.exportExcel(list, "健康登记数据");
    }

    /**
     * 新增健康登记
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存健康登记
     */
    @RequiresPermissions("system:health_register:add")
    @Log(title = "健康登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HealthRegister healthRegister)
    {
        return toAjax(healthRegisterService.insertHealthRegister(healthRegister));
    }

    /**
     * 修改健康登记
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        HealthRegister healthRegister = healthRegisterService.selectHealthRegisterById(id);
        mmap.put("healthRegister", healthRegister);
        return prefix + "/edit";
    }

    /**
     * 修改保存健康登记
     */
    @RequiresPermissions("system:health_register:edit")
    @Log(title = "健康登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HealthRegister healthRegister)
    {
        return toAjax(healthRegisterService.updateHealthRegister(healthRegister));
    }

    /**
     * 删除健康登记
     */
    @RequiresPermissions("system:health_register:remove")
    @Log(title = "健康登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(healthRegisterService.deleteHealthRegisterByIds(ids));
    }


    /**
     * 查看健康档案
     */
    @GetMapping("/query_health/{id}")
    public String queryHealth(@PathVariable("id") Long id, ModelMap mmap)
    {
        HealthRegister healthRegister = healthRegisterService.selectHealthRegisterById(id);
        mmap.put("healthRegister", healthRegister == null ? new HealthRegister(): healthRegister);
        return prefix + "/query";
    }



    /**
     * 修改健康档案
     */
    @GetMapping("/edit_health/{id}")
    public String health(@PathVariable("id") Long id, ModelMap mmap)
    {
        HealthRegister healthRegister = healthRegisterService.selectHealthRegisterById(id);
        mmap.put("healthRegister", healthRegister == null ? new HealthRegister(): healthRegister);
        return prefix + "/edit";
    }


    /**
     * 审核员审核通过
     * @param healthRegister
     * @return
     */
    @RequiresPermissions("system:health_register:edit")
    @Log(title = "登记审核通过", businessType = BusinessType.UPDATE)
    @PostMapping("/examine")
    @ResponseBody
    public AjaxResult examine(HealthRegister healthRegister){
        SysUser sysUser = ShiroUtils.getSysUser();
        boolean canExamine = false;
        HealthRegister oldHealthRegister = healthRegisterService.selectHealthRegisterById(healthRegister.getId());
        for(SysRole role : sysUser.getRoles()){
            if("normal_admin".equals(role.getRoleKey()) && Objects.equals(ExamineFlagEnum.SUBMIT.getCode(),oldHealthRegister.getFlag())){
                canExamine = true;
                healthRegister.setFlag(ExamineFlagEnum.PRELIMINARY_EXAMINE.getCode());
                break;
            }else if("senior_admin".equals(role.getRoleKey()) && Objects.equals(ExamineFlagEnum.PRELIMINARY_EXAMINE.getCode(),oldHealthRegister.getFlag())){
                canExamine = true;
                healthRegister.setFlag(ExamineFlagEnum.FINAL_EXAMINE.getCode());
                break;
            }else if("admin".equals(role.getRoleKey())){
                //can't examine
                continue;
            } else{
                continue;
            }
        }
        if(!canExamine){
            throw new RuntimeException("状态异常");
        }
        return toAjax(healthRegisterService.updateHealthRegister(healthRegister));
    }

    /**
     * 审核员审核拒绝
     * @param healthRegister
     * @return
     */
    @RequiresPermissions("system:health_register:edit")
    @Log(title = "登记审核拒绝", businessType = BusinessType.UPDATE)
    @PostMapping("/refuse")
    @ResponseBody
    public AjaxResult refuse(HealthRegister healthRegister){
        SysUser sysUser = ShiroUtils.getSysUser();
        boolean canExamine = false;
        HealthRegister oldHealthRegister = healthRegisterService.selectHealthRegisterById(healthRegister.getId());
        for(SysRole role : sysUser.getRoles()){
            if("normal_admin".equals(role.getRoleKey()) && Objects.equals(ExamineFlagEnum.SUBMIT.getCode(),oldHealthRegister.getFlag())){
                canExamine = true;
                healthRegister.setFlag(ExamineFlagEnum.PRELIMINARY_REFUSE.getCode());
                break;
            }else if("senior_admin".equals(role.getRoleKey()) && Objects.equals(ExamineFlagEnum.PRELIMINARY_EXAMINE.getCode(),oldHealthRegister.getFlag())){
                canExamine = true;
                healthRegister.setFlag(ExamineFlagEnum.FINAL_REFUSE.getCode());
                break;
            }else if("admin".equals(role.getRoleKey())){
                //can't examine
                continue;
            } else{
                continue;
            }
        }
        if(!canExamine){
            throw new RuntimeException("状态异常");
        }
        return toAjax(healthRegisterService.updateHealthRegister(healthRegister));
    }
}
